#include "dbwriter.h"
#include "dbconnection.h"
#include <QSqlResult>
#include <QSqlError>
#include <QSqlDriver>

DbWriter::DbWriter()
{
}


int DbWriter::addSingleRecord(SimpleRecord *record)
{
    QString err;
    if(record != 0)
    {
        QString sqlText = record->createInsertSql();
        QSqlQuery query;
        if(true == query.exec(sqlText))
        {            
            return _getLastInsertedId(query, record);
        }

        err = query.lastError().text();
    }

    return -1;
}

int DbWriter::_getLastInsertedId(QSqlQuery &lastQuery, SimpleRecord *record)
{
    QSqlDatabase db = DbConnection::instance()->getDb();
    if(db.driver()->hasFeature(QSqlDriver::LastInsertId))
    {
        return lastQuery.lastInsertId().toInt();
    }

    QString maxIdSql = record->createGetMaxIdSql();
    QSqlQuery maxIdQuery;
    if(maxIdQuery.exec(maxIdSql))
    {
        if(maxIdQuery.next())
        {
            QVariant maxId = maxIdQuery.value(0);
            return maxId.toInt();
        }
    }
    return -1;
}


bool DbWriter::addMultipleRecords(QVector<BulkInsertRecord *> &values)
{
    if(!values.empty())
    {
        QString queryText = values[0]->createInsertSqlPrefix();
        for(int i=0; i<values.size(); i++)
        {
            if(values[i] != 0)
            {
                queryText += values[i]->createValuesSql();
                if(i != values.size() -1)
                {
                    queryText += ", ";
                }
            }
        }

        QSqlQuery query;
        return query.exec(queryText);
    }

    return false;
}



/*
* record classes
*/


/*
* PolygonD
*/
QString PolygonRecord::createInsertSql()
{
    QString sqlText("INSERT INTO %1 (description, idMeasurementType) VALUES(\"%2\", \"%3\")");
    return sqlText.arg(POLYGON_TABLE_NAME, DbConnection::instance()->escapeValue(description), QString::number(idMeasurementType));
}

QString PolygonRecord::createGetMaxIdSql()
{
    QString sql = "SELECT max(idPolygon) maxId FROM " + QString(POLYGON_TABLE_NAME);
    return sql;
}


/*
* Emf
*/
QString EmfRecord::createValuesSql()
{
    return "(\"" + QString("%1").arg(idEmf) + "\", \"" +
             QString("%1").arg(time) + "\", \"" +
             QString("%1").arg(value) + "\")";
}

QString EmfRecord::createInsertSqlPrefix()
{
    return "INSERT INTO " + QString(EMF_TABLE_NAME) + "(idEmf, time, value) VALUES";
}

int EmfRecord::createId()
{
    QString sqlText = "SELECT max(idEmf) maxId FROM " + QString(EMF_TABLE_NAME);
    QSqlQuery sqlQuery;
    if(sqlQuery.exec(sqlText))
    {
        if(sqlQuery.next())
        {
            return sqlQuery.value(0).toInt() + 1;
        }
    }
    return -1;
}


/*
* Picket
*/
QString PicketRecord::createInsertSql()
{
    return createInsertSqlPrefix() + createValuesSql();
}

QString PicketRecord::createGetMaxIdSql()
{
    return "SELECT max(idPicket) maxId FROM " + QString(PICKET_TABLE_NAME);
}

QString PicketRecord::createValuesSql()
{
    QString sqlText = "(";
    sqlText += "\"" + QString("%1").arg(idGeneratorLoop) + "\", ";
    sqlText += "\"" + QString("%1").arg(idEmf) + "\", ";
    sqlText += "\"" + QString("%1").arg(x) + "\", ";
    sqlText += "\"" + QString("%1").arg(y) + "\", ";
    sqlText += "\"" + QString("%1").arg(z) + "\", ";
    sqlText += "\"" + QString("%1").arg(height) + "\"";
    sqlText += ")";

    return sqlText;
}


QString PicketRecord::createInsertSqlPrefix()
{
    return "INSERT INTO " + QString(PICKET_TABLE_NAME) + "(idGeneratorLoop, idEmf, x, y, z, height) VALUES";
}


/*
* Loop
*/
QString LoopReecord::createInsertSql()
{
    QString sqlText = "INSERT INTO " + QString(LOOP_TABLE_NAME) + "(idPolygon, idLoopForm, turnAngle, size, current, turnsNumber, centerX, centerY) VALUES(";

    sqlText += "\"" + QString("%1").arg(idPolygon) + "\", ";
    sqlText += "\"" + QString("%1").arg(idLoopForm) + "\", ";
    sqlText += "\"" + QString("%1").arg(turnAngle) + "\", ";
    sqlText += "\"" + QString("%1").arg(size) + "\", ";
    sqlText += "\"" + QString("%1").arg(current) + "\", ";
    sqlText += "\"" + QString("%1").arg(turnsNumber) + "\", ";
    sqlText += "\"" + QString("%1").arg(centerX) + "\", ";
    sqlText += "\"" + QString("%1").arg(centerY) + "\"";

    sqlText += ")";

    return sqlText;
}

QString LoopReecord::createGetMaxIdSql()
{
    return "SELECT max(idGeneratorLoop) maxId FROM " + QString(LOOP_TABLE_NAME);
}





